df_sub <- dta_midterm

df_sub$w9_2022_1_elections1_raw = df_sub$w9_2022_1_elections1
df_sub$w9_2022_1_elections1 <- recode(df_sub$w9_2022_1_elections1, '1' = "Agree", "2" = "Agree", "3" = "Don't Know", "4" = "Disagree", "5" = "Disagree")
df_sub$w9_2022_1_elections3 <- recode(df_sub$w9_2022_1_elections3, '1' = 'Yes', '2' = 'No', '3' = "Don't know")

apdx_res_pooled = df_sub |> 
  as_survey_design(ids = uid, weights = weight) |> 
  summarise(mean_acc = survey_mean(w9_2022_1_elections1_raw, vartype = "se", na.rm = T)) |> 
  mutate(pid = "Pooled")

apdx_res_pid = df_sub |> 
  as_survey_design(ids = uid, weights = weight) |> 
  group_by(pid) |> 
  summarise(mean_acc = survey_mean(w9_2022_1_elections1_raw, vartype = "se", na.rm = T)) 

apdx_res = bind_rows(apdx_res_pooled, apdx_res_pid) |> 
  mutate(pid = factor(pid, levels = c("Pooled","Republican", "Democrat", "Independent")))

fig_s1 = ggplot(apdx_res, aes(x = mean_acc, y = pid)) +
  geom_pointrange(aes(xmin = mean_acc - 1.96*mean_acc_se,
                      xmax = mean_acc + 1.96*mean_acc_se),
                  show.legend = F) +
  geom_text(aes(x = mean_acc + 1.96*mean_acc_se + .3,label = round(mean_acc, 2)), 
            fontface = "bold", vjust = .5, size = 4) +
  geom_vline(xintercept = 3, lty = 2, color = "red") +
  scale_x_continuous(limits = c(1,5), name = "Election Results Accurate",
                     breaks = 1:5, labels = c("Strongly\nAgree","Agree",
                                              "Neither Agree\nnor Disagree",
                                              "Disagree","Strongly\nDisagree")) +
  theme_prl() + 
  theme(panel.grid.major = element_blank(),
        plot.title = element_text(size=12),
        legend.text=element_text(size=9),          
        axis.title.y=element_blank(),
        axis.title.x=element_blank(), 
        legend.position = "none") +
  labs(caption = "Weighted Results and 95% CIs")

ggsave(here("Figures", "figure_s1.pdf"), fig_s1,
       width=6, height=3, units = 'in', dpi = 600)
